#include "tcpclient.h"



void TcpClient::run()
{
    socket = new QTcpSocket(this);
    socket->connectToHost("127.0.0.1", 8080);
    connect(socket,&QTcpSocket::readyRead,this,[this](){
        data += socket->readAll();
        int index=0;
        int l = data.size();
        while (index < l-11)
        {
            QByteArray t = data.mid(index,2);
            if(t == QByteArray::fromHex("F4A3"))
            {
                quint32 len = set_analysis1(data.mid(index+7,4))-5;//有效数据长度需要减去数据类型与数据长度共5个字节
                if( len <= l - 11)
                {
                    analysis(data.mid(index+11,len));
                    index = index + 11 + len;
                }else{
                    data = data.mid(index,l-index);
                    return;
                }
            }else
            {
                index++;
            }
        }
        data.clear();
    });
    connect(this, &TcpClient::sendMessage, this, [this](QJsonObject obj){
        // qDebug()<<"wt"<<obj;
        // socket->write(getSendJsondata(obj));

        QByteArray sendDataglobalConfig = QJsonDocument(obj).toJson();
        socket->write(sendDataglobalConfig);
    });
}


void TcpClient::sendMessages(QJsonObject obj)
{
    emit sendMessage(obj);
}

quint32 TcpClient::set_analysis1(QByteArray data)
{
    quint32 dd = 0;
    int i = data.size();
    char tp[4] = {};
    switch (i) {
    case 2:
    {
        qint16 v = *(reinterpret_cast<quint16*>(data.data()));
        qint16 i1 = qFromBigEndian<quint16>(v);
        dd = i1;
    }
    break;
    case 4:
    {
        tp[0] = data.at(0);
        tp[1] = data.at(1);
        tp[2] = data.at(2);
        tp[3] = data.at(3);
        qint32 i = qFromBigEndian<qint32>(tp);
        dd =  i;
    }
    break;
    default:
        break;
    }
    return dd;
}

void TcpClient::analysis(QByteArray data)
{

    QJsonParseError jsonError;
    QJsonDocument jsonDoc = QJsonDocument::fromJson(data, &jsonError);
    if(jsonError.error == QJsonParseError::NoError)
    {
        QJsonObject rootObj = jsonDoc.object();
        emit receiveMessage(rootObj);
        // QString filePath = jsonObj["filePath"].toString();
        // int listSize = jsonObj["listSize"].toInt();
        // QJsonArray listArray = jsonObj["list"].toArray();

        // // 输出数据
        // qDebug() << "FilePath:" << filePath;
        // qDebug() << "List Size:" << listSize;
        // qDebug() << "List:";
        // for (const QJsonValue &value : listArray) {
        //     qDebug() << value.toInt();
        // }

    }
}

QByteArray TcpClient::getSendJsondata(QJsonObject obj)
{
    QByteArray headTemprature;
    headTemprature.resize(11);
    headTemprature[0] = '\xf4';headTemprature[1] = '\xa3';
    QByteArray sendDataglobalConfig = QJsonDocument(obj).toJson();
    headTemprature[2] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0xff000000) >> 24);
    headTemprature[3] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0x00ff0000) >> 16);
    headTemprature[4] = (uint8_t)(((sendDataglobalConfig.size() + 11) & 0x0000ff00) >> 8);
    headTemprature[5] = (uint8_t)((sendDataglobalConfig.size() + 11) & 0x000000ff);
    headTemprature[6] = 1;
    headTemprature[7] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0xff000000) >> 24);
    headTemprature[8] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x00ff0000) >> 16);
    headTemprature[9] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x0000ff00) >> 8);
    headTemprature[10] = (uint8_t)(((sendDataglobalConfig.size() + 5) & 0x000000ff));
    sendDataglobalConfig  = headTemprature + sendDataglobalConfig;
    qDebug()<<"wt"<<sendDataglobalConfig;
    return sendDataglobalConfig;
}

